/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import fr.ove.openmath.mathematica.WorkBook;
import fr.ove.palette.swing.*;

/**
*
*/
public class MyMenuBar extends JMenuBar implements ChangeListener {
    /**
    * The WorkBook to operate with.
    */
    private WorkBook workBook;
    
    /**
    * The "File" menu. 
    */
    private JMenu fileMenu;
    
    /**
    * The "Edit" menu. 
    */
    private JMenu editMenu;
    
    /**
    * The "View" menu.
    */
    private JMenu viewMenu;
    
    /**
    * The "Window" menu.
    */
    private JMenu windowMenu;
    
    /**
    * Indicates if we have to add the "View" and "Window" menus.
    */
    private boolean addMenus = true;
    
    /**
    * The OpenMath viewer.
    */
    private OpenMathViewer omViewer;
    
    /**
    * The connection configuration dialog
    */
    private ConnectionConfigDialog ccd;
    
    /**
    * The Constructor.
    * @param workBook the WorkBook to operate with.
    */
    public MyMenuBar(WorkBook workBook, OpenMathViewer omViewer, ConnectionConfigDialog ccd) {
        this.workBook = workBook;
        this.omViewer = omViewer;
        this.ccd = ccd;
        
        createFileMenu();
        add(fileMenu);
        createEditMenu();
        add(editMenu);
        createViewMenu();
        createWindowMenu();
	}
    
    /**
    * Adds the "View" and "Window" menu.
    */
    public void addViewAndWindows() {
        if (addMenus) {
            add(viewMenu);
            add(windowMenu);
            addMenus = false;
            validate();
            repaint();
        }
    }
    
    /**
    * Removes the "View" and "Window" menu.
    */
    public void removeViewAndWindows() {
        if (!addMenus) {
            remove(viewMenu);
            remove(windowMenu);
            addMenus = true;
            validate();
            repaint();
        }
    }
    
    /**
    * Creates the "File" menu.
    */
    public void createFileMenu() {
        fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        
        // New item
        JMenuItem item = new JMenuItem("New");
        item.setMnemonic(KeyEvent.VK_N);
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/new.gif"));
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.addTab();
                }
            }
        );
        fileMenu.add(item);
        
        // Open... item
        item = new JMenuItem("Open...");
        item.setMnemonic(KeyEvent.VK_O);
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/open.gif"));
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.open();
                    
                }
            }
        );
        fileMenu.add(item);
        
        // Save item
        item = new JMenuItem("Save");
        item.setMnemonic(KeyEvent.VK_S);
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/save.gif"));
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.save();
                }
            }
        );
        fileMenu.add(item);
        
        // Save As... item
        item = new JMenuItem("Save As...");
        item.setMnemonic(KeyEvent.VK_A);
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/saveas.gif"));
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    
                }
            }
        );
        fileMenu.add(item);
        
        // Close item
        item = new JMenuItem("Close");
        item.setMnemonic(KeyEvent.VK_C);
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    
                }
            }
        );
        fileMenu.add(item);
        
        // A separator
        fileMenu.addSeparator();
        
        // Exit item
        item = new JMenuItem("Exit");
        item.setMnemonic(KeyEvent.VK_X);
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    System.exit(0);
                }
            }
        );
        fileMenu.add(item);
    }
    
    /**
    * Creates the "Edit" menu.
    */
    public void createEditMenu() {
        editMenu = new JMenu("Edit");
        editMenu.setMnemonic(KeyEvent.VK_E);
        
        // New item
        JMenuItem item = new JMenuItem("Insert");
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    JInternalFrame jif = workBook.getActiveInternalFrame();
                    OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
                    omSheet.insertNewElement();
                }
            }
        );
        editMenu.add(item);
        
        // A separator
        fileMenu.addSeparator();
        
        // New item
        item = new JMenuItem("Connection");
        /*
        item.setMnemonic(KeyEvent.VK_N);
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/new.gif"));
        */
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    ccd.setLocationRelativeTo(workBook);
                    ccd.show();
                }
            }
        );
        editMenu.add(item);
    }
    
    /**
    * Creates the "View" menu.
    */
    public void createViewMenu() {
        viewMenu = new JMenu("View");
        viewMenu.setMnemonic(KeyEvent.VK_V);
        
        // Palette Editor item
        JMenuItem item = new JMenuItem("Palette Editor");
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/palette.gif"));
        item.setMnemonic(KeyEvent.VK_P);
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.showPalette("Palette Editor");
                }
            }
        );
        viewMenu.add(item);
        
        // OpenMath item
        item = new JMenuItem("OpenMath");
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/show2.gif"));
        item.setMnemonic(KeyEvent.VK_M);
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    omViewer.setVisible(!omViewer.isVisible());
                    if (omViewer.isVisible())
                        updateOmViewer();
                }
            }
        );
        viewMenu.add(item);
    }
    
    /**
    * Creates the "Window" menu.
    */
    public void createWindowMenu() {
        windowMenu = new JMenu("Window");
        windowMenu.setMnemonic(KeyEvent.VK_W);
        
        // Cascade item
        JMenuItem item = new JMenuItem("Cascade");
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/cascade.gif"));
        item.setMnemonic(KeyEvent.VK_C);
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.cascade();
                }
            }
        );
        windowMenu.add(item);
        
        // Close All item
        item = new JMenuItem("Close All");
        item.setIcon(getIcon("fr/ove/openmath/mathematica/images/closeall.gif"));
        item.setMnemonic(KeyEvent.VK_L);
        item.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    workBook.closeAll();
                }
            }
        );
        windowMenu.add(item);
    }
    
    /**
    * Updates the OpenMath viewer.
    */
    private void updateOmViewer() {
        JInternalFrame jif = workBook.getActiveInternalFrame();
        OMWorkSheet omSheet = (OMWorkSheet) jif.getContentPane();
        PanelJomes panelJomes = omSheet.getCurrentElement();
        omViewer.setOpenMathRequest(panelJomes.getOpenMathRequest());
        omViewer.setOpenMathResult(panelJomes.getOpenMathResult());
    }
    
    /**
    * Returns the icon with the specified image filename.
    * @param filename the image filename
    */
    private ImageIcon getIcon(String filename) {
        ImageIcon icon = null;
        
        java.net.URL iconURL = ClassLoader.getSystemResource(filename);
        if (iconURL != null)
            icon = new ImageIcon(iconURL);
            
        return icon;
    }
    
    //  ### ChangeListenerInterface implementation
    
    /**
    * Invoked when the target of the listener has changed its state.
    * @param e a ChangeEvent object 
    */
    public void stateChanged(ChangeEvent e) {
        WorkBook wBook = (WorkBook) e.getSource();
        int count = wBook.getTabCount();
        if (count == 0)
            removeViewAndWindows();
        else
            addViewAndWindows();
    }
}